package generator;

import java.text.MessageFormat;

public class ParcelableBuilder extends SetterGetterBuilder {
	protected StringBuilder mWrite = new StringBuilder();
	protected StringBuilder mRead = new StringBuilder();

	@Override
	public void addMember(String visiblity, String type, String name, String description) {
		super.addMember(visiblity, type, name, description);
		// write
		String fieldName = getFieldName(name);
		if (type.equals("String")) {
			mWrite.append("dest.writeString(").append(fieldName);
		} else if (type.equals("Integer")) {
			mWrite.append("dest.writeInt(").append(fieldName);
		} else if (type.contains("HashMap")) {
			mWrite.append("dest.writeMap(").append(fieldName);
		} else if (type.equals("Bundle")) {
			mWrite.append("dest.writeBundle(").append(fieldName);
		} else if (type.equals("Parcelable")) {
			mWrite.append("dest.writeParcelable(").append(fieldName).append(", flags");
		} else if (type.equals("Short")) {
			mWrite.append("dest.writeInt(").append(fieldName);
		} else if (type.equals("Long")) {
			mWrite.append("dest.writeLong(").append(fieldName);
		} else if (type.equals("Float")) {
			mWrite.append("dest.writeFloat(").append(fieldName);
		} else if (type.equals("Double")) {
			mWrite.append("dest.writeDouble(").append(fieldName);
		} else if (type.equals("Boolean")) {
			mWrite.append("dest.writeInt(").append(fieldName).append(" ? 1 : 0");
		} else if (type.equals("CharSequence")) {
			mWrite.append("dest.writeString(").append(fieldName).append(".toString()");
		} else if (type.contains("ArrayList")) {
			mWrite.append("dest.writeList(").append(fieldName);
		} else if (type.contains("SparseArray")) {
			mWrite.append("dest.writeSparseArray(").append(fieldName);
		} else if (type.equals("byte[]")) {
			mWrite.append("dest.writeByteArray(").append(fieldName);
		} else if (type.equals("String[]")) {
			mWrite.append("dest.writeStringArray(").append(fieldName);
		} else if (type.equals("IBinder[]")) {
			mWrite.append("dest.writeBinderArray(").append(fieldName);
		} else if (type.equals("Parcelable[]")) {
			mWrite.append("dest.writeParcelableArray(").append(fieldName).append(", flags");
		} else if (type.equals("int[]")) {
			mWrite.append("dest.writeIntArray(").append(fieldName);
		} else if (type.equals("long[]")) {
			mWrite.append("dest.writeLongArray(").append(fieldName);
		} else if (type.equals("Byte")) {
			mWrite.append("dest.writeByte(").append(fieldName);
		} else if (type.equals("Serializable")) {
			mWrite.append("dest.writeSerializable(").append(fieldName);
		} else if (type.equals("SparseBooleanArray")) {
			mWrite.append("dest.writeSparseBooleanArray(").append(fieldName);
		} else if (type.equals("boolean[]")) {
			mWrite.append("dest.writeBooleanArray(").append(fieldName);
		} else if (type.equals("int")) {
			mWrite.append("dest.writeInt(").append(fieldName);
		} else {
			mWrite.append("dest.writeUnknownType(").append(fieldName);
		}
		mWrite.append(");\r\n        ");

		// read
		if (type.equals("String")) {
			mRead.append(fieldName).append(" = ").append("in.readString();");
		} else if (type.equals("Integer")) {
			mRead.append(fieldName).append(" = ").append("in.readInt();");
		} else if (type.contains("HashMap")) {
			mRead.append(fieldName).append(" = ").append("in.readHashMap(HashMap.class.getClassLoader());");
		} else if (type.equals("Bundle")) {
			mRead.append(fieldName).append(" = ").append("in.readBundle();");
		} else if (type.equals("Parcelable")) {
			mRead.append(fieldName).append(" = ").append("in.readParcelable(").append(type).append(".class.getClassLoader()").append(");");
		} else if (type.equals("Short")) {
			mRead.append(fieldName).append(" = ").append("(short) in.readInt();");
		} else if (type.equals("Long")) {
			mRead.append(fieldName).append(" = ").append("in.readLong();");
		} else if (type.equals("Float")) {
			mRead.append(fieldName).append(" = ").append("in.readFloat();");
		} else if (type.equals("Double")) {
			mRead.append(fieldName).append(" = ").append("in.readDouble();");
		} else if (type.equals("Boolean")) {
			mRead.append(fieldName).append(" = ").append("in.readInt() == 1 ? true : false;");
		} else if (type.equals("CharSequence")) {
			mRead.append(fieldName).append(" = ").append("in.readString();");
		} else if (type.contains("ArrayList")) {
			mRead.append(fieldName).append(" = ").append("in.readArrayList(ArrayList.class.getClassLoader());");
		} else if (type.contains("SparseArray")) {
			mRead.append(fieldName).append(" = ").append("in.readSparseArray(SparseArray.class.getClassLoader());");
		} else if (type.equals("byte[]")) {
			mRead.append("in.readByteArray((").append(fieldName).append(" = new byte[0])").append(");");
		} else if (type.equals("String[]")) {
			mRead.append("in.readStringArray((").append(fieldName).append(" = new String[0])").append(");");
		} else if (type.equals("IBinder[]")) {
			mRead.append("in.readBinderArray((").append(fieldName).append(" = new IBinder[0])").append(");");
		} else if (type.equals("Parcelable[]")) {
			mRead.append(fieldName).append(" = ").append("in.readParcelableArray(Parcelable[].class.getClassLoader());");
		} else if (type.equals("int[]")) {
			mRead.append("in.readIntArray((").append(fieldName).append(" = new int[0])").append(");");
		} else if (type.equals("long[]")) {
			mRead.append("in.readLongArray((").append(fieldName).append(" = new long[0])").append(");");
		} else if (type.equals("Byte")) {
			mRead.append(fieldName).append(" = ").append("in.readByte();");
		} else if (type.equals("Serializable")) {
			mRead.append(fieldName).append(" = ").append("in.readSerializable();");
		} else if (type.equals("SparseBooleanArray")) {
			mRead.append(fieldName).append(" = ").append("in.readSparseBooleanArray();");
		} else if (type.equals("boolean[]")) {
			mRead.append("in.readBooleanArray((").append(fieldName).append(" = new boolean[0])").append(");");
		} else if (type.equals("int")) {
			mRead.append(fieldName).append(" = ").append("in.readInt();");
		} else {
			mRead.append(fieldName).append(" = ").append("in.readUnknownType();");
		}
		mRead.append("\r\n        ");
	}

	@Override
	public String build() {
		StringBuilder source = new StringBuilder();
		source.append("package {0};");
		source.append("\r\n");
		source.append("\r\n");
		source.append("\r\n").append("/** {1} */");
		source.append("\r\n").append("public class {2} implements {6} {3}");
		source.append("\r\n").append("{4}");
		source.append("\r\n").append("    {7}");
		source.append("\r\n").append("{5}");

		return MessageFormat.format(source.toString(),
				getPkg(),
				getDescription(),
				getName(),
				"{",
				mField.toString() + mMethod.toString(),
				"}",
				"android.os.Parcelable",
				getParcelable());
	}

	private String getParcelable() {
		String Parcelable = "@Override"
				+ "\r\n    public int describeContents() {3}"
				+ "\r\n        return 0;"
				+ "\r\n    {4}"
				+ "\r\n"
				+ "\r\n    @Override"
				+ "\r\n    public void writeToParcel(Parcel dest, int flags) {3}"
				+ "\r\n        {0}"
				+ "\r\n    {4}"
				+ "\r\n"
				+ "\r\n    /***/"
				+ "\r\n    public static final Parcelable.Creator<{2}> CREATOR = new Parcelable.Creator<{2}>() {3}"
				+ "\r\n        public {2} createFromParcel(Parcel in) {3}"
				+ "\r\n            return new {2}(in);"
				+ "\r\n        {4}"
				+ "\r\n"
				+ "\r\n        public {2}[] newArray(int size) {3}"
				+ "\r\n            return new {2}[size];"
				+ "\r\n        {4}"
				+ "\r\n    {4};"
				+ "\r\n"
				+ "\r\n    private {2}(Parcel in) {3}"
				+ "\r\n        {1}"
				+ "\r\n    {4}";
		return MessageFormat.format(Parcelable, mWrite.toString(), mRead.toString(), getName(), "{", "}");

	}
}
